<?php

class RssCategoryManager
{
    public static function GetTableName()
    {
        return Util::GetDatabaseTableName("rss_tag");
    }

    public static function GetRssCategoryById( $id )
    {
        $sql = "SELECT * FROM rss_tag WHERE rss_tag_id = %s LIMIT 1";
        $data = Core::DBConn()->Query( $sql, Util::Quote($id) )->GetAssoc();
        $result = ( !empty($data) ) ? self::CreateRssCategory($data) : null;
        return $result;
    }

    public static function GetCategoriesByPostId( $postId )
    {
        $sql = "SELECT rss_category.* FROM rss_category, rss_post_category
        		WHERE
        		  rss_category.rss_category_id = rss_post_category.rss_category_id
        		AND
        		  rss_post_category.rss_post_id = %s";
        $categoriesAssocs = Core::DBConn()->Query($sql, $postId)->GetAllAssoc();

        $categories = array();
        foreach ($categoriesAssocs as $data)
        {
            $category = self::CreateRssCategory($data);
            $category->SetPostId($postId);
            $categories[] = $category;
        }

        return $categories;
    }

    public static function Insert( RssCategory $rssCategory )
    {
        $tableName = self::GetTableName();
        $sql = "INSERT IGNORE INTO $tableName SET title = %s";
        Core::DBConn()->Query( $sql, Util::Quote($rssCategory->GetTitle()) );

        $rssCategoryId = Core::DBConn()->GetLastInsertId();
        $rssPostId = $rssCategory->GetPostId();

        self::InsertIntoRssPostCategory($rssCategoryId, $rssPostId);

        $rssCategory->SetCategoryId($rssCategoryId);
        return $rssCategoryId;
    }

    public static function UpdateRssCategories($postId, $rssCategories)
    {
        RssPostCategoryManager::DeletePostCategories($postId);
        self::InsertRssCategories($postId, $rssCategories);
    }

    public static function InsertRssCategories($postId, $rssCategories)
    {
        if ( !is_array($rssCategories) || empty($rssCategories) )
        {
            return;
        }

        $categoryTitles = self::GetTitlesArray($rssCategories);
        $categoryTitles = Util::QuoteArray($categoryTitles);
        self::InsertTitles($categoryTitles);
        $categoryIds = self::GetCategoryIdsByTitles($categoryTitles);
        RssPostCategoryManager::InsertIds($postId, $categoryIds);
    }

    private static function InsertTitles( $categoryTitles )
    {
        $tableName = self::GetTableName();
        $sql = "INSERT IGNORE INTO $tableName (title) VALUES ";
        $categoriesCount = count($categoryTitles);
        for ($i = 0; $i < $categoriesCount; $i++)
        {
            $title = $categoryTitles[$i];
            $sql .= "('" . $title . "')";

            $isLast = ( $i == ($categoriesCount - 1) );
            $sql .= ( !$isLast ) ? ", " : "";
        }
        Core::DBConn()->Query($sql);
    }

    private static function GetCategoryIdsByTitles( $titles )
    {
        $tableName = self::GetTableName();
        $sql = "SELECT rss_tag_id FROM $tableName WHERE ";
        $titlesCount = count($titles);
        for ($i = 0; $i < $titlesCount; $i++)
        {
            $sql .= "title = '" . $titles[$i] . "'";

            $isLast = ( $i == ($titlesCount - 1) );
            $sql .= ( !$isLast ) ? " OR " : "";
        }


        $allAssoc = Core::DBConn()->GetResults($sql, ARRAY_A);
        $categoryIds = array();

        foreach ($allAssoc as $row)
        {
            array_push($categoryIds, $row['rss_tag_id']);
        }

        return $categoryIds;
    }

    public static function Update( RssCategory $rssCategory )
    {
        $sql = "UPDATE rss_tag
        		SET
        		  title = @title
        		WHERE
        		  rss_tag_id = @rss_tag_id";
        $params = array(
            "@rss_category_id" => $rssCategory->GetCategoryId(),
            "@title"           => $rssCategory->GetTitle()
        );
        Core::DBConn()->Query( $sql, Util::QuoteArray($params) );

        return $rssCategory->GetCategoryId();
    }

    public static function Delete( $id )
    {
        $sql = "DELETE FROM rss_category WHERE rss_category_id = %s";
        Core::DBConn()->Query( $sql, Util::Quote($id) );
    }

    public static function GetTitlesArray( $rssCategories )
    {
        $titlesArray = array();
        foreach ( $rssCategories as $rssCategory )
        {
            $titlesArray[] = strtolower($rssCategory->GetTitle());
        }
        return $titlesArray;
    }

    private static function CreateRssCategory( $data )
    {
        $rssCategory = new RssCategory();

        $rssCategory->SetCategoryId($data['rss_category_id']);
        $rssCategory->SetTitle($data['title']);

        return $rssCategory;
    }
}

?>